function  [bdraw,m1,CC1,nit]=drawnpos(y,x,prm,prprec,sig,ind,tol);
%function [bdraw,m1,CC1,nit]=drawnpos(y,x,prm,prprec,sig,ind,tol); 
% Draw Coefficients from a Normal distribution when prior is also 
%      Normal( prm, prprec^(-1) )  and prprec is the precision
%      Ind coefficient restricted to be greater than tol 
% 
% y      (Txn) 
% x      (TxK)  
% Prm    (Kx1)  Prior Mean 
% prprec (KxK)  Prior Precision
% sig    (1x1)  Variance of the errors
% position indicator of coefficient to be restricted
% tol     bound, e.g. 0.001 or eps  for positive 
% 
% bdraw  (1xK)  Draw from the posterior normal as a row vector 
% m1     (1xK)  Posterior mean 
% CC1    (KxK)  Cholelsky of the posterior variance 
%               V1= CC1'*CC1  
% nit    Number of draws until constrained satisfied 

% Remove to run faster 
% [ty,ny]=size(y); 
% [tx,nx]=size(x); 
% if tx~=ty
%     error('Not equal number of observations in drawcoef'); 
% end; 
nk=length(prm);
V1=( prprec+ (1/sig)*(x'*x)  ) \ eye(nk) ;   % Posterior Variance  
m1=V1*(prprec*prm + (1/sig)*(x'*y) );        % Posterior Mean  
CC1=chol(V1); 
acc=0;
nit=0; 
rcoun=0; 
while acc < 1 
    bdraw=m1'+ randn(1,nk)*CC1;
    if bdraw(ind) > tol 
        acc=1 ; 
    end
    nit=nit+1; 
    if nit == 10000; 
        nit=0; 
        rcoun=rcoun+1; 
        warning('Non-negative restriction requires more than 10000 draws...');
        if rcoun==5; 
            error('More than 50000 draws could not satisfy the constraint');  
        end
    end
end 
m1=m1'; 